package com.feizi.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.feizi.entity.AddressEntity;
import com.feizi.entity.UserEntity;
import com.feizi.service.AddressService;


@Controller
public class AddressController {
	
	@Autowired
	private AddressService addressService;
	
	
	/**
	 * 新增地址
	 * @param session
	 * @param addressEntity
	 * @param ids
	 * @return
	 */
	@RequestMapping(path = "/addAddress.do",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addAddress(HttpSession session,AddressEntity addressEntity) {
		UserEntity user = (UserEntity) session.getAttribute("user");
		addressEntity.setF_userid(user.getUserid());
		//调用新增
		int rows = addressService.addAddress(addressEntity);
		Map<String, Object> rslMap = new HashMap<String, Object>();
		rslMap.put("code", rows>0? "0" : "1");
		rslMap.put("msg", rows>0? "新增成功" : "新增失败");
		
		return rslMap;
		
	}
	
	/**
	 * 删除地址
	 * @param session
	 * @param addressEntity
	 * @param id
	 * @return
	 */
	@RequestMapping(path = "/deleteAddress.do",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteProduct(HttpSession session,Integer id) {	
		//调用删除
		int rows = addressService.delAddress(id);
		Map<String, Object> rslMap = new HashMap<String, Object>();
		rslMap.put("code", rows>0? "0" : "1");
		rslMap.put("msg", rows>0? "删除成功" : "删除失败");
		
		return rslMap;
	}
	/**
	 * 修改地址
	 * @param session
	 * @param addressEntity
	 * @return
	 */
	@RequestMapping(path = "/editAddress.do",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> editProduct(HttpSession session,AddressEntity addressEntity) {
		UserEntity user = (UserEntity) session.getAttribute("user");
		addressEntity.setF_userid(user.getUserid());
		//调用修改
		int rows = addressService.upAddress(addressEntity);
		Map<String, Object> rslMap = new HashMap<String, Object>();
		rslMap.put("code", rows>0? "0" : "1");
		rslMap.put("msg", rows>0? "修改成功" : "修改失败");
		
		return rslMap;
	}

}
